﻿using System.Text;
using System.Collections.Specialized;
using Newtonsoft.Json;

namespace CometDocs
{
    internal partial class Client
    {
        /// <summary>
        /// Get the list of all the CometDocs online storage public files in a given category or all the categories if none is provided: https://www.cometdocs.com/developer/apiDocumentation#method-getPublicFiles
        /// </summary>
        /// <param name="token">A valid authentication token.</param>
        /// <param name="category">A category.</param>
        /// <returns>An array of files.</returns>
        public FileInfo[] GetPublicFiles(AuthenticationToken token, Category category = null)
        {
            NameValueCollection @params = new NameValueCollection(2);
            @params.Add("token", token.Value);
            if (category != null)
            {
                @params.Add("categoryId", category.ID.ToString());
            }

            byte[] result = webClient.UploadValues(APIRoot + "getPublicFiles", @params);

            string json = Encoding.ASCII.GetString(result);

            GetPublicFilesResponse response = JsonConvert.DeserializeObject<GetPublicFilesResponse>(json);

            CheckAndThrow(response);

            return response.Files;
        }
    }
}
